Flow Of Control

Flow of Control statements, available in nShell-Pro(tm) control when and how other statements are executed.  That is, they control the flow of execution through a command line or through a script file.

When flow of control structures are typed into the command line, they must start and end within the same line:

if yesno "ready?" then hello endif

When flow of control structures are used in scripts, they may be broken into several lines:

if yesno "ready?"
then

hello
endif

if then else

This structure has the following form:

if <commands1> then <commands2> [else <commands3>] endif

If the result of the last command of the set <commands1> completes without error (result = zero), then the <commands2> clause will be executed.

If the result of the last command of the set <commands1> produces an error (result <> zero), and an else-clause is present, that clause will execute.

while do done

This structure has the following form:

while <commands1> do <commands2> done

In this structure, the <commands1> clause will always execute.  If the result of the last command of the set completes without error (result = zero), then the <commands2> clause will be executed.  Control will then return to the <commands1> clause, and the process will continue until the last command of this set produces an error (result <> 0).

until do done

This structure has the following form:

until <commands1> do <commands2> done

In this structure, the <commands1> clause will always execute.  If the result of the last command of the set produces an error (result <> zero), then the <commands2> clause will be executed.  Control will then return to the <commands1> clause, and the process will continue until the last command of this set completes without error (result = 0).

Testing Conditions

A number of commands are included in this release to test conditions.  The commands below produce a successful result (zero) when the listed condition is true:

.eq.
Equal

.ne.
Not Equal

.gt.
Greater Than

.lt.
Less Than

.ge.
Greater Than or Equal To

.le.
Less Than or Equal To

These operators are used to compare two parameters.  If both parameters are legal integers, an integer comparison is performed.  If one or both parameters are not integers, a string comparison is performed.

These comparison commands may be used to test the return value from a previous command.  It is important to note, however, that the comparison itself produces a return value and will overwrite "$?".  For this reason, the return code should be saved into a temporary variable before a series of comparisons are performed:

command_x
set temp $?
# save the result
if .lt. $temp 0 then echo "A serious error has occurred." endif
if .eq. $temp 0 then echo "Everything is ok" endif
if .gt. $temp 0 then echo "command_x returned a value of $temp" endif